<template>
  <div>
    <MyTable :arr="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{row}">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
           type="text"
           class="tag-input form-control"
           style="width: 100px;"
           v-if="row.inputVisible"
           v-focus
           v-model="row.inputValue"
           @blur="row.inputVisible = false"
           @keydown.esc="row.inputValue = ''"
           @keydown.enter="enterFn(row)">
          <button style="display: block;" 
          class="btn btn-primary btn-sm add-tag"
          v-else
          @click="row.inputVisible = true">+tag</button>
          <span v-for="(str, ind) in row.tags" :key="ind" class="badge bg-warning"> {{ str }} </span>
        </td>
        <td><button @click="delFn(row.id)" class="btn btn-danger btn-sm">删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable.vue"
export default {
  components: {
    MyTable
  },
  data() {
    return {
      list: [],
    }
  },
  methods: {
    delFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    enterFn(item) {
      if (item.inputValue.trim() === "") {
        alert("请输入内容")
        return
      };
      item.tags.push(item.inputValue)
      item.inputValue = ""
    }
  },
  created() {
    this.$axios({
      url: "/api/goods"
    }).then(res => {
      console.log(res)
      this.list = res.data.data
    }).catch(err => {
      console.log(err)
    })
  }
}
</script>

<style>
.badge{
  margin-right: 5px;
}
</style>